//
//  ACLiveFileListViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/3/30.
//

#import "ACLiveFileListViewController.h"
#import "ACFileListCell.h"
#import "ACLiveModel.h"
#import "ACFileInfoViewController.h"


@interface ACLiveFileListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray<ACFileModel*>*dataArray;
@property(nonatomic,assign)int page;

@end

@implementation ACLiveFileListViewController
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.estimatedRowHeight = 109;
        _tableview.backgroundColor = UIColorFromRGB(0xF4F5F8);
        NSBundle *curB = [NSBundle BundleWithBundleName:ACLiteAVUISDK targetClass:[self class] ];
        [_tableview registerNib:[UINib nibWithNibName:@"ACFileListCell" bundle:curB] forCellReuseIdentifier:@"ACFileListCell"];
        _tableview.tableFooterView = [UIView new];
    }
    return _tableview;
}
-(NSMutableArray<ACFileModel *> *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)selectedArray{
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColorFromRGB(0xF4F5F8);
    self.title = @"文件管理";
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
    }];
    self.page = 1;
    @weakify(self)
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.page = 1;
        [self requestData];
    }];
    self.tableview.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [self requestData];
    }];
    self.selectedArray = [NSMutableArray array];
}
//请求文件列表
-(void)getFiles{
    NSString *url = [HOSTURL stringByAppendingString:@"/api/v1/material/material"];
    //self 0查询全部\1查询自己的
    NSDictionary *params = @{@"page_size":@"20",@"page":[NSString stringWithFormat:@"%d",self.page],@"self":@"1"};
    [[ACNetWorkTool shareRequestManager] get:url params:params success:^(id  _Nonnull responseObject) {
        NSArray *dataArr = [responseObject valueForKey:@"data"];
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
            self.dataArray = [ACFileModel mj_objectArrayWithKeyValuesArray:dataArr];
            [self.tableview.mj_header endRefreshing];
        }else{
            [self.dataArray addObjectsFromArray:[ACFileModel mj_objectArrayWithKeyValuesArray:dataArr]];
            [self.tableview.mj_footer endRefreshing];
        }
        [self filterDataArray];
        } failure:^(NSError * _Nonnull error) {
            [self.tableview.mj_header endRefreshing];
            [self.tableview.mj_footer endRefreshing];
            [SVProgressHUD showInfoWithStatus:@"网络错误"];
    }];
}

-(void)filterDataArray{
    [self.selectedArray enumerateObjectsUsingBlock:^(NSString * _Nonnull file_id, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.dataArray enumerateObjectsUsingBlock:^(ACFileModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([file_id isEqualToString:obj.id_file]) {
                obj.isSelected = @"1";
                *stop = YES;
                return;
            }
        }];
    }];
    [self.tableview reloadData];
}

-(void)setLiveID:(NSString *)liveID{
    _liveID = liveID;
    [self requestData];
}
-(void)requestData{
    [SVProgressHUD show];
    [ACLiveRoomManager requestFileListWithLiveID:self.liveID success:^(id  _Nonnull responseObject) {
        [SVProgressHUD dismiss];
        [self.tableview.mj_header endRefreshing];
        NSArray *dic = [responseObject valueForKey:@"materials"];
        self.dataArray = [ACFileModel mj_objectArrayWithKeyValuesArray:dic];
        [self.tableview reloadData];
    } failure:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ACFileListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACFileListCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[ACFileListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ACFileListCell"];
    }
    [cell setBtnStyle:self.index];
    ACFileModel *fileModel = self.dataArray[indexPath.row];
    cell.fileModel = fileModel;
    @weakify(self)
    cell.clickActionHandler = ^(BOOL isSelected) {
        @strongify(self)
        if (self.index == 2) {
            ACFileInfoViewController *fileInfo = [[ACFileInfoViewController alloc] init];
            fileInfo.id_file = fileModel.id_file;
            [self.navigationController pushViewController:fileInfo animated:YES];
        }else{
            fileModel.isSelected = [NSString stringWithFormat:@"%d",isSelected];
            if (isSelected) {
                [self.selectedArray addObject:fileModel.id_file];
            }else{
                if ([self.selectedArray containsObject:fileModel.id_file]) {
                    [self.selectedArray removeObject:fileModel.id_file];
                }
            }
        }
    };
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ACFileModel *fileModel = self.dataArray[indexPath.row];
    ACFileInfoViewController *fileInfo = [[ACFileInfoViewController alloc] init];
    fileInfo.id_file = fileModel.id_file;
    [self.navigationController pushViewController:fileInfo animated:YES];
}

#pragma mark - JXCategoryListContentViewDelegate

/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}

@end
